<?php

    namespace app\api\service;


    use app\common\model\AccountHasVoucher;
    use app\common\model\Order;

    use app\api\service\Order as OrderService;
    use app\lib\enum\OrderStatusEnum;
    use think\Db;
    use think\Exception;
    use think\facade\Env;
    use think\Loader;
    use think\facade\Log;

    require_once(Env::get('extend_path') . '/NewWxPay/WxPay.Api.php');
    require_once(Env::get('extend_path') . '/NewWxPay/WxPay.Notify.php');

    class WxNotify extends \WxPayNotify
    {
        //    protected $data = <<<EOD
        //<xml><appid><![CDATA[wxaaf1c852597e365b]]></appid>
        //<bank_type><![CDATA[CFT]]></bank_type>
        //<cash_fee><![CDATA[1]]></cash_fee>
        //<fee_type><![CDATA[CNY]]></fee_type>
        //<is_subscribe><![CDATA[N]]></is_subscribe>
        //<mch_id><![CDATA[1392378802]]></mch_id>
        //<nonce_str><![CDATA[k66j676kzd3tqq2sr3023ogeqrg4np9z]]></nonce_str>
        //<openid><![CDATA[ojID50G-cjUsFMJ0PjgDXt9iqoOo]]></openid>
        //<out_trade_no><![CDATA[A301089188132321]]></out_trade_no>
        //<result_code><![CDATA[SUCCESS]]></result_code>
        //<return_code><![CDATA[SUCCESS]]></return_code>
        //<sign><![CDATA[944E2F9AF80204201177B91CEADD5AEC]]></sign>
        //<time_end><![CDATA[20170301030852]]></time_end>
        //<total_fee>1</total_fee>
        //<trade_type><![CDATA[JSAPI]]></trade_type>
        //<transaction_id><![CDATA[4004312001201703011727741547]]></transaction_id>
        //</xml>
        //EOD;

        //1 检测库存量（万一超卖？怎么解决）
        //2 更新订单status状态
        //3 减库存
        // 4 成功 返回成功消息 ，失败 返回没有成功
        public function NotifyProcess($data,$config, &$msg)
        {
            $data=$data->GetValues();
            if ($data['result_code'] == 'SUCCESS') {
                Log::record('成功进入回调');
                //            获取订单号
                $orderNo = $data['out_trade_no'];
                Db::startTrans();
                try {
                    $order = Order::where('order_no', '=', $orderNo)->lock(true)->find();
                    //status =1 为订单未处理状态
                    if ($order->state == 1) {
                        $order->state = 2;
                        $order->save();
                        $this->updateVoucher($order->account_has_voucher_id);
                    }
                    Db::commit();
                    return true;
                } catch (Exception $ex) {
                    Db::rollback();
                    Log::error($ex);
                    // 如果出现异常，向微信返回false，请求重新发送通知
                    return false;
                }
            }
            return true;
        }

        /**削减库存量
         *
         * @param $status
         */
        private function reduceStock($status)
        {
            //        $pIDs = array_keys($status['pStatus']);
            foreach ($status['pStatusArray'] as $singlePStatus) {
                Product::where('id', '=', $singlePStatus['id'])
                       ->setDec('stock', $singlePStatus['count']);
            }
        }

        /**更新订单状态 待支付 或者 库存量不足
         *
         * @param $orderID
         * @param $success
         */
        private function updateOrderStatus($orderID, $success)
        {
            $status = $success ? OrderStatusEnum::PAID : OrderStatusEnum::PAID_BUT_OUT_OF;
            Order::where('id', '=', $orderID)
                 ->update(['status' => $status]);
    }

        private function updateVoucher($voucherId){
            AccountHasVoucher::where('id',$voucherId)->update(['is_used'=>1]);
        }
    }